//
// api.js
// vue3-element-admin
//
// Created by 区区电脑 on 2023/04/11.
//

import request from "@/request";

// 添加菜单
export function AddMenuInfo(data = {}) {
    return request({url: `/api/SysMenu/AddMenuInfo`, method: 'post', data})
}

// 删除菜单
export function DeleteMenuInfo(params = {}) {
    return request({url: `/api/SysMenu/DeleteMenuInfo`, method: 'delete', params})
}

// 修改菜单
export function UpdateMenuInfo(data = {}) {
    return request({url: `/api/SysMenu/UpdateMenuInfo`, method: 'put', data})
}

// 查询菜单列表
export function GetMenuList(params = {}) {
    return request({url: `/api/SysMenu/GetMenuList`, method: 'get', params})
}

// 查询菜单树
export function GetMenuTreeList(params = {}) {
    return request({url: `/api/SysMenu/GetMenuTreeList`, method: 'get', params})
}

/**
 * 查询菜单详情
 * @param params
 * @returns {Promise | Promise<unknown> | *}
 * @constructor
 */
export function GetMenuInfo(params = {}) {
    return request({url: `/api/SysMenu/GetMenuInfo`, method: 'get', params})
}

/**
 * 查询系统接口列表
 * @param params
 * @returns {Promise | Promise<unknown> | *}
 * @constructor
 */
export function GetSystemApiList(params = {}) {
    return request({url: `/api/SysMenu/GetSystemApiList`, method: 'get', params}).then(res => {
        if (res.success && res.data) {
            console.primaryText("displayName 为空的数量 ", res.data.filter(item => !item.displayName).length)
            const groupedData = res.data.reduce((acc, current) => {
                const key = current.groupName;
                if (!acc[key]) {
                    acc[key] = [];
                }
                acc[key].push(current);
                return acc;
            }, {});
            const result = [];
            for (const key in groupedData) {
                result.push({
                    name: key,
                    children: groupedData[key]
                });
            }
            res.data = result;
        }
        return res
    })
}
